<template>
  <el-container>
    <!-- 头部 -->
    <div v-if="headShow" class="header">
      <!-- header-search组件 -->
      <header-search @reset="handlerReset" @search="handlerSearch">
        <template #search>
          <el-form-item small>
            <template slot="label">
              <div class="label">标签名称</div>
            </template>
            <el-input
              ref="searchRef"
              v-model="searchData.catename"
              size="small"
              placeholder="请输入标签名称"
            />
          </el-form-item>
        </template>
      </header-search>
    </div>
    <!-- 按钮组件 -->
    <header-button
      :head-show.sync="headShow"
      :check-data.sync="checkData"
      @open="openDialog"
      @loading="handlerLoading"
    />
    <!-- 表格 -->
    <el-table
      v-loading="loading"
      border
      :data="tableData"
      style="margin-top: 14px"
      :header-cell-style="{ color: '#646464' }"
      type="index"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
    >
      <el-table-column label="#" width="60" type="index" />
      <el-table-column v-if="checkData[0].isckecked" label="分类ID" prop="id" />
      <el-table-column v-if="checkData[1].isckecked" label="分类名称" prop="catename" />
      <el-table-column v-if="checkData[2].isckecked" label="图标class" prop="icon" />
      <el-table-column v-if="checkData[3].isckecked" label="权重" prop="sort_num" />
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            @click="handlerEdit('edit', row.id)"
          />
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            @click="handlerDel(row.id)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-footer>
      <lay-page
        :limit.sync="searchData.limit"
        :page.sync="searchData.page"
        :count="count"
      />
    </el-footer>

    <!-- 对话框 -->
    <!-- title="添加分类"  -->
    <el-dialog :visible.sync="visible" width="45%" :before-close="handleClose">
      <!-- 表格 -->
      <div slot="title" class="header-title">
        <!-- <div>添加分类</div> -->
        <div>{{ title }}</div>
      </div>
      <el-form
        ref="formRef"
        label-width="120px"
        size="medium"
        :model="formData"
        :rules="rules"
      >
        <el-form-item prop="catename">
          <template #label>
            <span class="title"> 分类名称 </span>
          </template>
          <el-input
            v-model="formData.catename"
            size="medium"
            placeholder="请输入分类名"
            style="width: 550px"
          />
        </el-form-item>
        <el-form-item prop="icon">
          <template #label>
            <span class="title"> 图标class </span>
          </template>
          <el-input
            v-model="formData.icon"
            size="medium"
            placeholder="请输入class名"
            style="width: 200px"
          />
          <span style="margin-left: 20px">具体参见fontwesome图标库</span>
        </el-form-item>
        <el-form-item prop="sort_num">
          <template #label>
            <span class="title"> 排序大小 </span>
          </template>
          <el-input-number

            v-model="formData.sort_num"
            :min="1"
          />
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" autofocus @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </el-container>
</template>

<script>
import {
  getCateList,
  addCate,
  cateDel,
  cateInfo,
  cateEdit
} from '@/api/content'
export default {
  name: 'CatePage',
  data() {
    return {
      loading: false, // loading 加载
      headShow: true, // 头部显示隐藏
      title: '',
      tableData: [],
      searchData: {
        catename: '',
        page: 1,
        limit: 10
      },
      count: 0,
      totalPages: 0,
      visible: false, // 对话框的状态
      drawerType: '', // 类型 添加/修改
      formData: {
        catename: '',
        sort_num: 10,
        icon: ''
      },
      formType: '',
      // searchContent: '',
      rules: {
        catename: [
          { required: true, message: '分类名称类型必须！', trigger: 'blur' }
        ],
        icon: [
          { required: true, message: '分类图标class必须！', trigger: 'blur' }
        ]
      },
      checkData: [
        { id: 1, name: '分类ID', isckecked: true },
        { id: 2, name: '分类名称', isckecked: true },
        { id: 3, name: '图表class', isckecked: true },
        { id: 4, name: '权重', isckecked: true }
      ]
    }
  },
  // 分页
  watch: {
    'searchData.limit'(val) {
      this.initList()
    },
    'searchData.page'(val) {
      this.initList()
    }
  },
  created() {
    this.initList()
  },
  methods: {
    async initList() {
      const res = await getCateList(this.searchData)
      console.log(res)
      this.tableData = res.data
      this.count = res.count
      this.totalPages = res.totalPages
    },
    openDialog(type) {
      console.log(type)
      this.title = '添加分类'
      this.formType = type
      this.visible = true
    },
    async submit() {
      await this.$refs.formRef.validate()
      if (this.formType === 'add') {
        console.log(this.formData)
        await addCate(this.formData)
        this.$message.success('添加分类成功')
      } else {
        console.dir(this.formData)
        await cateEdit(this.formData)
        this.$message.success('修改分类成功')
      }

      this.initList()

      this.dialogClose()
    },
    // 关闭对话框 清空表单
    dialogClose() {
      this.$refs.formRef.clearValidate()
      this.visible = false
      this.formData = {
        catename: '',
        sort_num: 10,
        icon: ''
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          this.dialogClose()
        })
        .catch((_) => {})
    },

    // 搜索分类详情
    handlerSearch() {
      if (this.searchData.catename.length === 0) return this.$message('请输入内容')
      this.initList()
    },
    handlerReset() {
      this.searchData.catename = ''
      this.initList()
    },
    // 删除
    handlerDel(id) {
      this.$confirm('确认删除？')
        .then(async(_) => {
          await cateDel(id)
          this.$message.success('删除成功')
          if (this.searchData.page > 1 && this.tableData.length === 1) {
            this.searchData.page--
          }
          this.initList()
        })
        .catch((_) => {})
    },
    async handlerEdit(type, id) {
      console.log(type, id)
      this.formType = type

      this.title = '修改分类'
      const res = await cateInfo(id)
      this.visible = true
      this.formData = {
        ...res,
        id
      }
    },
    // 设置刷新loading
    async handlerLoading() {
      this.tableData = []
      this.loading = true
      await this.initList()
      this.loading = false
    }
  }
}
</script >

<style lang="less" scoped>
.el-container {
  background-color: #fff;
  padding: 20px;
}
.el-input {
  width: 230px;
}

.label {
  font-size: 14px;
  color: #606266;
  font-weight: 700;
}
.box {
  display: flex;
  justify-content: space-between;
}
.el-pagination {
  text-align: right;
  margin-top: 20px;
}
.title {
  font-size: 14px;
  font-weight: 700;
}

/deep/.el-dialog__header {
  background: #fafafa;
}
</style>
